package com.administrator.customviewtest.test3;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.util.SparseArrayCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.administrator.customviewtest.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/5/19.
 */

public class RecyclerViewHeadAndFootTest extends Activity {
    private RecyclerView recyclerView;
    private List<String> list = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rv_test);

        recyclerView = (RecyclerView) findViewById(R.id.rv);

        for (int i = 0; i < 10; i++) {
            list.add(i + "");
        }

        recyclerView.setLayoutManager(new LinearLayoutManager(this));


        NormalAdapter normalAdapter = new NormalAdapter();

        MyAdapter myAdapter = new MyAdapter(normalAdapter);

        myAdapter.addHead(View.inflate(this,R.layout.item_head,null));
        myAdapter.addHead(View.inflate(this,R.layout.item_head,null));
        myAdapter.addFoot(View.inflate(this,R.layout.item_head,null));

        recyclerView.setAdapter(myAdapter);

    }

    public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
        private RecyclerView.Adapter innerAdapter;
        private SparseArrayCompat<View> headsView = new SparseArrayCompat<>();
        private SparseArrayCompat<View> footView = new SparseArrayCompat<>();
        private static final int head = 1000;
        private static final int foot = 2000;

        public MyAdapter(RecyclerView.Adapter adapter) {
            innerAdapter = adapter;
        }
        public void addHead(View v){
            headsView.put(headsView.size()+head,v);
        }

        public void addFoot(View v){
            footView.put(footView.size()+foot,v);
        }

        @Override
        public int getItemViewType(int position) {
            if (position < headsView.size()) {
                return headsView.keyAt(position);
            } else if (position >= innerAdapter.getItemCount() + headsView.size()) {
                return footView.keyAt(position - headsView.size() - innerAdapter.getItemCount());
            }

            return innerAdapter.getItemViewType(position);
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            Log.e("ssss",viewType+"");
            if (headsView.get(viewType) != null) {
                return new ViewHolder(View.inflate(parent.getContext(), R.layout.item_head, null));

            } else if (footView.get(viewType) != null) {
                return new ViewHolder(View.inflate(parent.getContext(), R.layout.item_head, null));

            }
            return innerAdapter.onCreateViewHolder(parent, viewType);
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

            if (position < headsView.size()) {
                //头部
                return;
            } else if (position >= innerAdapter.getItemCount() + headsView.size()) {
                return;
            }

            innerAdapter.onBindViewHolder(holder, position-headsView.size());
        }

        @Override
        public int getItemCount() {
            return headsView.size() + footView.size() + innerAdapter.getItemCount();
        }
    }


    public class NormalAdapter extends RecyclerView.Adapter<ViewHolder> {


        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new ViewHolder(View.inflate(parent.getContext(), R.layout.item_test, null));
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            holder.textView.setText(list.get(position));
        }

        @Override
        public int getItemCount() {
            return list.size();
        }

    }

    public class Adapter extends RecyclerView.Adapter<ViewHolder> {
        private static final int head = 1;
        private static final int normal = 2;

        @Override
        public int getItemViewType(int position) {
            return position == 0 ? head : normal;
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            switch (viewType) {
                case head:
                    return new ViewHolder(View.inflate(parent.getContext(), R.layout.item_head, null));
                case normal:
                    return new ViewHolder(View.inflate(parent.getContext(), R.layout.item_test, null));
            }
            return null;
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            if (position == 0) {
                return;
            }
            holder.textView.setText(list.get(position - 1));
        }

        @Override
        public int getItemCount() {
            return list.size() + 1;
        }

    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView textView;

        public ViewHolder(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.tv);
        }
    }
}
